package org.aiforum.backend.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aiforum.backend.common.CookieHelper;
import org.aiforum.backend.common.LoginRequest;
import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.AdminService;
import org.aiforum.domain.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 管理员控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
   private AdminService adminService;
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value={"/logout"})
	public String logout(HttpServletRequest request,HttpServletResponse response){
		//获取token
		String token=CookieHelper.getCookie("token", request);
		//退出(删除管理员对象)
		adminService.logout(token);
		//清空token
		CookieHelper.setCookie("token", null, response);
		//重定向到登录页
		return "redirect:../login";
	}
	
	/**
	 * 登录
	 * @param loginRequest
	 * @return
	 */
	@RequestMapping(value={"/login"},method={RequestMethod.POST})
	@ResponseBody
	public Response Login(LoginRequest loginRequest){
		Response response=adminService.login(loginRequest);
		return response;
	}
	
	/**
	 * 根据管理员权限异步获取管理员权限ID集合
	 * @param adminId
	 * @return
	 */
	@RequestMapping(value={"/auths/{adminId}"},method={RequestMethod.GET})
	@ResponseBody
	public Response auths(@PathVariable Integer adminId){
		List<Integer> auths=adminService.auths(adminId);
		Response response=Response.getInstaner();
		response.setState(auths.size()>0);
		response.setData(auths);
		return response;
	}
	/**
	 * 异步检查管理员账号和邮箱是否存在
	 * @param queryPreperty
	 * @return
	 */
	@RequestMapping(value={"/checkExists"},method={RequestMethod.GET})
	@ResponseBody
	public Response exists(QueryPreperty queryPreperty){
		Response response=adminService.checkExists(queryPreperty);
		return response;
	}
	
	/**
	 * 插入管理员及其权限
	 * @param admin
	 * @param authIds
	 * @return
	 */
	@RequestMapping(value={"/insert"},method={RequestMethod.POST})
	public ModelAndView insert(Admin admin,int[] authIds){
		Response response=adminService.insert(admin, authIds);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("redirect:../message");
		modelAndView.addObject("message", response.getMessage());
		return modelAndView;
	}
	
	@RequestMapping(value={"/query"})
	public ModelAndView query(QueryPreperty queryPreperty,PageInfo page){
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
		List<Admin> admins=adminService.queryByPreperty(queryPreperty);
		page=new PageInfo<Admin>(admins);
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("admin/list");
		modelAndView.addObject("admins", admins);
		modelAndView.addObject("queryPreperty", queryPreperty);
		modelAndView.addObject("page", page);
		return modelAndView;
	}
	
	@RequestMapping(value={"/find/{adminId}"},method={RequestMethod.GET})
	public ModelAndView find(@PathVariable Integer adminId){
		Admin admin=adminService.find(adminId);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("admin/update");
		modelAndView.addObject("admin", admin);
		return modelAndView;
	}
	
	@RequestMapping(value={"/modify"},method={RequestMethod.POST})
	public ModelAndView modify(Admin admin,int[] authIds){
		
		//插入管理员
		Response response=adminService.update(admin, authIds);
		//创建视图模型
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("redirect:../message");
		modelAndView.addObject("message", response.getMessage());
		return modelAndView;
	}
	
	@RequestMapping(value={"/delete/{adminId}"},method={RequestMethod.GET})
	public ModelAndView delete(@PathVariable Integer adminId){
		Response response=adminService.delete(adminId);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("redirect:../../message");
		modelAndView.addObject("message", response.getMessage());
		return modelAndView;
		
	}
}
